package com.wisezone.workflow.bean.search;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.wisezone.common.bean.search.BaseSearch;

public class WorkFlowSearch extends BaseSearch{

	/**
	 * ID
	 */
	private Long id;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3014169552388648637L;
	
	/**
	 * 流程名称
	 */
	private String name;

	/**
	 * 流程实例
	 */
	private Long processInstId;

	/**
	 * 发起时间
	 */
	private Date startTime;

	/**
	 * 发起人ID
	 */
	private Long startUserId;

	/**
	 * 状态ID
	 */
	private String statusId;
	
	/**
	 * 活动实例ID
	 */
	private Long activityInstId;
	

    /**
     * 任务参与者
     */
    private Long actorId;

    /**
     * 用户组ID
     */
    private String actorGroupIds = "-1";
    
    /**
     * 用户IDs
     */
    private Long [] userIds;
    
    /**
     * 用户组IDS
     */
    private Long [] userGroupIds;
    
    /**
     * 流程标识
     */
    private String key;
    
    /**
     * 意见内容
     */
    private String opinion;
    
    /**
     * 跳转链接ID
     */
    private Long linkId;
    
    /**
     * 全局变量参数
     */
    private Map<String,Object> variableMap = new HashMap<String,Object>();
    
    /**
     * 局部变量参数
     */
    private Map<String,Object> localVariableMap = new HashMap<String,Object>();
    
    
	 /**
	  * 下一个用户IDs
	  */
	 private String nextUserIds;
	 
	 /**
	  * 下一个用户组IDs
	  */
	 private String nextUserGroupIds;

	public Long getActorId() {
		return actorId;
	}

	public void setActorId(Long actorId) {
		this.actorId = actorId;
	}

	public Long getLinkId() {
		return linkId;
	}

	public void setLinkId(Long linkId) {
		this.linkId = linkId;
	}

	public String getActorGroupIds() {
		return actorGroupIds;
	}

	public void setActorGroupIds(String actorGroupIds) {
		this.actorGroupIds = actorGroupIds;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	

	public Map<String, Object> getVariableMap() {
		return variableMap;
	}

	public void setVariableMap(Map<String, Object> variableMap) {
		this.variableMap = variableMap;
	}
	
	public Long[] getUserIds() {
		return userIds;
	}

	public void setUserIds(Long[] userIds) {
		this.userIds = userIds;
	}

	public Long[] getUserGroupIds() {
		return userGroupIds;
	}

	public void setUserGroupIds(Long[] userGroupIds) {
		this.userGroupIds = userGroupIds;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getProcessInstId() {
		return processInstId;
	}

	public void setProcessInstId(Long processInstId) {
		this.processInstId = processInstId;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Long getStartUserId() {
		return startUserId;
	}

	public void setStartUserId(Long startUserId) {
		this.startUserId = startUserId;
	}

	public String getStatusId() {
		return statusId;
	}

	public void setStatusId(String statusId) {
		this.statusId = statusId;
	}

	public Long getActivityInstId() {
		return activityInstId;
	}

	public void setActivityInstId(Long activityInstId) {
		this.activityInstId = activityInstId;
	}

	public String getOpinion() {
		return opinion;
	}

	public void setOpinion(String opinion) {
		this.opinion = opinion;
	}
	

	public Map<String, Object> getLocalVariableMap() {
		return localVariableMap;
	}

	public void setLocalVariableMap(Map<String, Object> localVariableMap) {
		this.localVariableMap = localVariableMap;
	}


	public String getNextUserIds() {
		return nextUserIds;
	}

	public void setNextUserIds(String nextUserIds) {
		this.nextUserIds = nextUserIds;
	}
	
	
	
	public String getNextUserGroupIds() {
		return nextUserGroupIds;
	}

	public void setNextUserGroupIds(String nextUserGroupIds) {
		this.nextUserGroupIds = nextUserGroupIds;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getProcessInstSql(){
		
		if(actorId == null){
			throw new IllegalArgumentException("用户ID 不能用空");
		}
		StringBuffer sb = new StringBuffer();
		sb.append(" wf_process_instance t2,wf_activity_instance t3   where t2.id = t3.process_inst_id ");
		sb.append("and t1.process_inst_id = t2.id and  t3.id in  (select (activity_inst_id) from ");
		sb.append("wf_activity_inst_user where ((user_id is not null and user_id = "+actorId+" ) or (user_id is ");
		sb.append("null  and user_group_id in ("+actorGroupIds+") ) and del_state = 1 ) ) and t3.id = (select max(activity_inst_id) from ");
		sb.append("wf_activity_inst_user where process_inst_id = t2.id  and  (user_id = "+actorId+" ");
		sb.append("or user_group_id in ("+actorGroupIds+") ) and del_state = 1  )  ");
		return sb.toString();
	}
	
	public String getProcessInstOrderBySql(){
		
		return " order by case when t3.end_time is null then 1 else 2 end ,t2.start_time desc";
	}
    
    
	public String getProcessInstHql(String hibHql){
		
		//Hql 处理
		int whereIndex = hibHql.indexOf("where");
		String mainHql = hibHql;
		String whereHql = " and ti.processInstId = t.procsssInstId ";
		if(whereIndex != -1){
			mainHql = hibHql.substring(0,whereIndex);
			whereHql = whereHql + " and "+ hibHql.substring(whereIndex+5,hibHql.length());
		}
		StringBuffer sb = new StringBuffer();
		sb.append(mainHql);
		sb.append(",WfActivityInstance ti,WfProcessInstance t2 ");
		
		//已操作
		sb.append("where ti.processInstId = t2.id and ((ti.userId is not null and ti.userId = :actorId " );
		
		//已操作过滤重复记录
		sb.append(" and ti.createTime in (select max(ti2.createTime) from WfActivityInstance ti2 where ti2.userId = ti.userId and ti2.processInstId = ti.processInstId ))");
		
		//未操作
		sb.append(" or (ti.assignee is  null and ti.id in ");
		
		//未操做过滤任务人员条件
		sb.append("(select tu.activityId from WfActivityInstUser tu where ((tu.userId is null and tu.roleId in ( :roleIds ) ) or (tu.userId is not null and tu.userId = :actorId )))))");
		
		sb.append(whereHql);
		
		sb.append(" order by case when ti.endTime is null then 1 else 2 end ,t.startTime desc");
		
		hibHql = "select new WorkFlowForm(t,ti,t2) "+sb.toString();
		return hibHql;
	}
	
	
	
}
